package cn.gxljc.technology.store_management.service.loginservice;


import cn.gxljc.technology.store_management.entity.*;
import cn.gxljc.technology.store_management.mapper.*;
import cn.gxljc.technology.store_management.service.loginservice.modules.LoginServiceMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 登录服务：专门处理所有有关登录的业务
 */
@Component
public class LoginService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private AccountInfoMapper accountInfoMapper;

    @Resource
    private AccountPermissionsMapper accountPermissionsMapper;

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private LoginServiceMapper loginServiceMapper;

    @Resource
    private WarehouseManagementMapper warehouseManagementMapper;

    //查找账号是否存在
    private Integer isAccountExist(Integer accountID) {
        Account account = accountMapper.selectByAccountID(accountID);
        if (account == null) return -1;
        return 0;
    }

    //判断密码是否正确
    private Integer isPasswordCorrect(Integer accountID, String password) {
        Account account = accountMapper.selectByAccountID(accountID);
        if (account == null) return -1;
        String password1 = account.getPassword();
        if (password1 == null) return -1;
        if (!password.equals(password1)) return -1;
        return 0;
    }

    //判断账号是否存在权限
    private Integer isAuthorityExist(Integer accountID) {
        AccountPermissions accountPermissions = accountPermissionsMapper.selectByAccountID(accountID);
        if (accountPermissions == null) return -1;
        ArrayList<Integer> typeList = new ArrayList<>();
        typeList.add(0);
        typeList.add(1);
        typeList.add(2);
        int permissions = accountPermissions.getPermissions();
        if (!typeList.contains(permissions)) return -1;
        return 0;
    }

    /**
     * 功能：获取账号的权限
     */
    public Integer getAuthority(String accountID) {
        //非空判断
        if (accountID == null || accountID.equals("")) {
            return null;
        }
        int accountIDS;
        try {//账号格式判断
            accountIDS = Integer.parseInt(accountID);
        } catch (NumberFormatException e) {
            return null;
        }
        AccountPermissions accountPermissions = accountPermissionsMapper.selectByAccountID(accountIDS);
        return accountPermissions.getPermissions();
    }

    /**
     * 功能：获取账号用户信息
     */
    public AccountInfo getAccountInfo(String accountID) {
        //非空判断
        if (accountID == null || accountID.equals("")) {
            return null;
        }
        int accountIDS;
        try {//账号格式判断
            accountIDS = Integer.parseInt(accountID);
        } catch (NumberFormatException e) {
            return null;
        }
        AccountInfo accountInfo = accountInfoMapper.selectByAccountID(accountIDS);
        if (accountInfo.getAccountName() == null || accountInfo.getAccountName().equals(""))
            accountInfo.setAccountName("/用户名为空/");
        if (accountInfo.getPhoneNumber() == null || accountInfo.getPhoneNumber().equals(""))
            accountInfo.setPhoneNumber("/手机号为空/");
        if (accountInfo.getEmail() == null || accountInfo.getEmail().equals(""))
            accountInfo.setEmail("/邮箱为空/");
        return accountInfo;
    }

    /**
     * 功能：获取用户管理仓库信息
     */
    public Warehouse getWarehouse(String accountID) {
        //非空判断
        if (accountID == null || accountID.equals("")) {
            return null;
        }
        int accountIDS;
        try {//账号格式判断
            accountIDS = Integer.parseInt(accountID);
        } catch (NumberFormatException e) {
            return null;
        }
        WarehouseManagement warehouseManagement = warehouseManagementMapper.selectWMByAccountID(accountIDS);
        if (warehouseManagement == null) {
            Warehouse warehouse = new Warehouse();
            warehouse.setWarehouseID(-1);
            warehouse.setWarehouseName("/没有分配到仓库/");
            warehouse.setWarehouseAddress("/没有分配到仓库/");
            return warehouse;
        }

        int warehouseID = warehouseManagement.getWarehouseID();

        Warehouse warehouse = warehouseMapper.selectWarehouseByID(warehouseID);
        String warehouseName = warehouse.getWarehouseName();
        String warehouseAddress = warehouse.getWarehouseAddress();
        if (warehouseName == null || warehouseName.equals("")) warehouse.setWarehouseName("/仓库名为空/");
        if (warehouseAddress == null || warehouseAddress.equals("")) warehouse.setWarehouseAddress("/仓库地理位置为空/");
        return warehouse;
    }

    /**
     * 功能：获取账号权限信息
     * 输入参数：accountID（账号ID）
     * 输出参数：AccountPermission(对象)
     */
    public AccountPermissions getAccountPermission(String accountID) {
        if (accountID == null || accountID.equals("")) {
            return null;
        }
        int accountIDS;
        try {
            accountIDS = Integer.parseInt(accountID);
        } catch (NumberFormatException e) {
            return null;
        }
        return accountPermissionsMapper.selectByAccountID(accountIDS);
    }

    /**
     * 功能：主要用于登录后获得用户基本信息
     * 输入参数：accountID（账号ID）；password（密码）；
     * 输出参数：result（0登录成功，-1登录失败，-2账号不存在，-3密码不正确，-4创建账号时没有设置账号权限）
     */
    public Integer loginUser(String accountID, String password) {
        if (accountID == null || password == null || accountID.equals("") || password.equals("")) {
            return -1;
        }
        Integer accountIDS;
        try {
            accountIDS = Integer.parseInt(accountID);
        } catch (NumberFormatException e) {
            return -1;
        }
        Integer accountExist = isAccountExist(accountIDS);
        if (accountExist == -1) {
            return -2;
        }
        Integer passwordCorrect = isPasswordCorrect(accountIDS, password);
        if (passwordCorrect == -1) {
            return -3;
        }
        Integer authorityExist = isAuthorityExist(accountIDS);
        if (authorityExist == -1) {
            return -4;
        }
        return 0;
    }

    /**
     * 功能：主要用于处理超级管理员的登录
     * 输入参数：账号，密码
     * 输出参数：0登录成功，-1登录失败，-2不存在该账号，-3密码不正确，-4没有给账号设置权限，-5不是超级管理员账户
     */
    private Integer login(long accountID, String password, Integer isType) {
        //非空判断
        if (password == null || isType == null) {
            System.out.println("LoginService/loginSuperAdmin/参数为空");
            return -1;
        }

        //查找是否存在该账号
        Account account = accountMapper.selectByAccountID(accountID);
        if (account == null) {
            System.out.println("LoginService/loginSuperAdmin/不存在该账号");
            return -2;
        }

        //判断账号中的密码是否正确
        String password1 = account.getPassword();
        if (!password1.equals(password)) {
            System.out.println("LoginService/loginSuperAdmin/密码错误");
            return -3;
        }

        //判断账号是否有设置权限
        AccountPermissions authority = loginServiceMapper.findAuthority(accountID);
        //AccountPermissions accountPermissions = accountPermissionsMapper.selectByAccountID(accountID);
        if (authority == null) {
            System.out.println("LoginService/loginSuperAdmin/该账户未授权登录");
            return -4;
        }

        //判断是否是对应的管理员权限
        if (authority.getPermissions() != isType) {
            System.out.println("LoginService/loginSuperAdmin/判断是否是对应的管理员权限");
            return -5;
        }

        return 0;
    }

    /**
     * 功能：主要用于处理超级管理员的登录
     * 输入参数：账号，密码
     * 输出参数：0登录成功，-1登录失败，-2不存在该账号，-3密码不正确，-4没有给账号设置权限，-5不是超级管理员账户
     */
    public Integer loginSuperAdmin(long accountID, String password) {
        return login(accountID, password, 0);
    }

    /**
     * 功能：主要用于处理仓库管理员的登录
     * 输出参数：0登录成功，-1登录失败，-2不存在该账号，-3密码不正确，-4没有给账号设置权限，-5不是仓库管理员账户
     */
    public Integer loginRepositoryAdmin(long accountID, String password) {
        return login(accountID, password, 1);
    }

    /**
     * 功能：主要用于卖家的登录
     * 输出参数：0登录成功，-1登录失败，-2不存在该账号，-3密码不正确，-4没有给账号设置权限，-5不是卖家账户
     */
    public Integer loginCustomer(long accountID, String password) {
        return login(accountID, password, 2);
    }

}
